// subPages/business/evaluation/evaluation.js
const app = getApp();
var util =  require('../../../utils/util.js');
var loadMoreView,p = 1,num = app.globalData.pageNum,type = 'evaluation'
var flag = false
Page({

  /**
   * 页面的初始数据
   */
  data: {
    downImg: '/resources/business/down.png',
    upImg: '/resources/business/up.png',
    date: '',
    isDate: false,
    isGoodss: false,
    status: 0,
    productId: '',
    evaluationId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      dateImgSrc: this.data.downImg,
      goodsImgSrc: this.data.downImg
    })
    loadMoreView = this.selectComponent("#loadMoreView")
    this.setDate()
    this.getGoodsList()
    this.getData()
  },

  //设置日期
  setDate:function(){
    let dateStart = util.setTime(new Date())
    let Y = dateStart.split('-')
    this.setData({
      dateStart: Y[0]- 3,
      dateEnd: dateStart,
    })
  },

  bindDateChange: function(e) {
    this.setData({
      isDate: true,
      date: e.detail.value,
      dateImgSrc: this.data.downImg
    })
    this.getData()
  },

  bindDateCancel:function(){
    this.setData({
      dateImgSrc: this.data.downImg
    })
  },

  //社区选择
  bindGoodsChange:function(e){
    // console.log(e)
    var that = this
    if(that.data.goodsArray){
      that.setData({
        goodsIndex: e.detail.value,
        goods: that.data.goodsArray[e.detail.value],
        goodsImgSrc: that.data.downImg,
        isGoods: true,
        productId: that.data.goodsArray[e.detail.value].id
      })
      this.getData()
    }
  },

  bindGoodsCancel:function(){
    this.setData({
      goodsImgSrc: this.data.downImg
    })
  },

  //选择箭头
  imgChange:function(e){
    var that = this
    var type = e.currentTarget.dataset.type
    switch (type) {
      case 'date':
        if(that.data.dateImgSrc == that.data.downImg){
          that.setData({
            dateImgSrc: that.data.upImg
          })
        }
        break;
      case 'goods':
        if(that.data.goodsImgSrc == that.data.downImg){
          that.setData({
            goodsImgSrc: that.data.upImg
          })
        }
        break;
    }
  },

  //删除标签
  dateDelete:function(){
    this.setData({
      isDate: false,
      date: ''
    })
    this.getData()
  },

  //删除标签
  goodsDelete:function(){
    this.setData({
      isGoods: false,
    })
    this.getData()
  },

  //获取商品列表
  getGoodsList:function(){
    var that = this
    //获取商品数据
    util.getAjax(`${app.globalData.apiUrl}/admin_get_health_shop_product`,function(res){
      // console.log(res)
      if(res.data.list.length != 0){
        that.setData({
          goodsArray: res.data.list,
        })
      }
    },{p: 1,num: 999999999,class_id: '',type: 1,like: '',shop_id: app.globalData.shopId})
  },

  //评论状态按钮
  statusBtn:function(e){
    var that = this
    var type = e.currentTarget.dataset.type;
    that.setData({
      status: type
    })
    that.getData()
  },

  //获取评论数据
  getData:function(){
    var that = this
    var start_time = '',end_time = ''
    if(that.data.date){
      start_time = (new Date(String(that.data.date).replace(/-/g,"/") + ' 00:00:00').getTime())/1000
      end_time = (new Date(String(that.data.date).replace(/-/g,"/") + ' 23:59:59').getTime())/1000
    }
    util.getAjax(`${app.globalData.apiUrl}/Api/Admincommunityhealthshop/get_community_health_shop_tell`,that.getEvaluationData,{p:p,num:num,product_id: that.data.productId,status: that.data.status,start_time: start_time,end_time: end_time,shop_id: app.globalData.shopId},that.getEvaluationErr)
  },

  getEvaluationData(res){
    var that = this
    console.log(res)
    if(res.data.list.length != 0){
      for(var i in res.data.list){
        if(res.data.list[i].content_img && res.data.list[i].content_img != 0){
          for(let j in res.data.list[i].content_img){
            res.data.list[i].content_img[j] = app.globalData.apiUrl + res.data.list[i].content_img[j]
          }
        }
      }
      var list = []
      if(p == 1) {
        list = res.data.list
      } else {
        list = that.data.evaluationList.concat(res.data.list)
      }
      that.setData({
        evaluationList: list
      })
    }else{
      that.setData({
        evaluationList: []
      })
    }
    loadMoreView.loadMoreComplete(res.data,type)
  },

  //获取商品数据异常
  getEvaluationErr(err){
    if(p>1){
      loadMoreView.loadMoreFail()
    }else{
      util.showModal('提示','获取评论失败，请向上滑动屏幕')
      this.setData({
        isFail: true
      })
    }
  },

  loadMoreListener: function(e) {
    p += 1
    this.getData()
  },

  clickLoadMore: function(e) {
    this.getData()
  },

  //点击图片
  imgBtn:function(e){
    var list = e.currentTarget.dataset.list;
    var index = e.currentTarget.dataset.idx;
    wx.previewImage({
      urls: list,
      current: list[index]
    })
  },

  //回复
  replyBtn:function(e){
    this.setData({
      evaluationId: e.currentTarget.dataset.id,
      isReply: true
    })
  },

  //确认回复
  replyOk:function(e){
    // console.log(e)
    if(flag){
      return
    }
    flag = true
    var that = this
    var replyTxt = e.detail.value
    util.getAjax(`${app.globalData.apiUrl}/Api/Admincommunityhealthshop/tell`,function(res){
      console.log(res)
      if(res.data == 1){
        //成功 
        util.showToast('回复成功','success',2000)
        that.setData({
          isReply: false,
          evaluationId: ''
        })
        flag = false
        that.getData()
      }else{
        flag = false
        util.showToast('回复失败，请重试','none',2000)
      }
    },{fid: that.data.evaluationId,content: replyTxt})
  },

  //阻止穿透
  preventTouchMove: function () {
    // console.log('stop user scroll it!');
    return;
  },

  //阻止点击
  preventTouch:function(){
    this.setData({
      isReply: false,
      evaluationId: ''
    })
  },

  //回到顶部
  goTop:function (e) {
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },

  // 获取滚动条当前位置
  onPageScroll: function (e) {
    // console.log(e)
    if (e.scrollTop > 600) {
      this.setData({
        isTop: true
      });
    } else {
      this.setData({
        isTop: false
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getData()
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.isFail){
      this.getData()
      return
    }
    loadMoreView.loadMore()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})